package com.mygdx.game.actor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * DemoActor
 * Created by 80002023 on 2016/8/11.
 */
public class DemoActor extends Actor {

    Texture texture1;

    Animation animation;

    TextureRegion[] walksFrame;

    float stateTime;

    @Override
    public void draw(Batch batch, float parentAlpha) {
        stateTime += Gdx.graphics.getDeltaTime();
        TextureRegion currentFrame = animation.getKeyFrame(stateTime, true);
        batch.draw(currentFrame, this.getX(), getY());
    }

    @Override
    public Actor hit(float x, float y, boolean touchable) {
        Gdx.app.log("DemoActor", x + "-" + this.getWidth());
        if (x > 0 && y > 0 && getWidth() > x && getHeight() > y) {
            return this;
        }
        return null;
    }

    public DemoActor() {
        texture1 = new Texture(Gdx.files.internal("data/Mario.png"));
        TextureRegion[][] mario = TextureRegion.split(texture1, 64, 64);
        walksFrame = new TextureRegion[3];
        walksFrame[0] = mario[0][1];
        walksFrame[1] = mario[0][2];
        walksFrame[2] = mario[0][0];
        animation = new Animation(0.15f, walksFrame);
        setWidth(64);
        setHeight(64);
    }
}
